<?php
function __autoload($class_name) {
    global $g_factory;
    require_once $g_factory->base_constants["path"]["libs"] . "/" . $class_name . '.php';
}


#######################################################################################################################
## BASE FACTORY PART ##################################################################################################
#######################################################################################################################


class base_factory {

    protected $_extend_variables = array();

    /*-READ-ONLY PROPERTIES----------------------------------------------------------------------*/
    protected $_root_path;
    /** This function get the read-only root path
     * @return the absolute path to root app directory (ex: 'C:/home/app'), remember that the path end without '/'
     */
    public function get_rootpath() { return $this->_root_path; }
    /*-END READ-ONLY PROPERTIES------------------------------------------------------------------*/

    /** CONSTRUCTOR METHOD
     */
    public function __construct() {
        $this->_root_path = preg_replace("#/*\z#", "", preg_replace("#[\\\\]#", "/", realpath(dirname(__FILE__) . "/../")));
    }

    /*-OVERLOADING METHODS-----------------------------------------------------------------------*/
    public function __set($name, $value) { $this->_extend_variables[$name] = $value; }
    public function __get($name) {
        if (array_key_exists($name, $this->_extend_variables)) return $this->_extend_variables[$name];
        $trace = debug_backtrace();
        trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE);
        return null;
    }
    public function __isset($name) { return isset($this->_extend_variables[$name]); }
    public function __unset($name) { unset($this->_extend_variables[$name]); }
    /*-END OVERLOADING METHODS-------------------------------------------------------------------*/
}


#######################################################################################################################
## EXTENDED FACTORY PART ##############################################################################################
#######################################################################################################################


class extend_factory extends base_factory {

    public $base_constants;
    public $extend_constants;

    /** CONSTRUCTOR METHOD
     * @param string|array $path_to_factory_configs: relative path to single or multiple configs.php file(s)
     */
    public function __construct($path_to_factory_configs = "/configs/configs.php") {
        parent::__construct();
        if (is_string($path_to_factory_configs)) include($this->_root_path . $path_to_factory_configs);
        elseif (is_array($path_to_factory_configs))
            foreach ($path_to_factory_configs as $path) include($this->_root_path . $path);
    }
}


#######################################################################################################################
## FULL FACTORY PART ##################################################################################################
#######################################################################################################################


class full_factory extends extend_factory {

    /*-READ-ONLY PROPERTIES----------------------------------------------------------------------*/
    private $_smarty; public function get_smarty() { return $this->_smarty; }
    /*-END READ-ONLY PROPERTIES------------------------------------------------------------------*/

    /** render the smarty for buiding template of the page
     * @param string $TPLfile: path to TPL file
     * @param array $smartyVars: array contain variables assigned to template
     * @param bool $isAssignedExtendVars: is the extend variables of base_factory assigned to template variables
     * @param bool $isAssignedExtendConst: is the extend constants of extend_factory assigned to template variables
     */
    public function render_smarty($TPLfile, $smartyVars = array(), $isAssignedExtendVars = false, $isAssignedExtendConst = true) {
        if ($isAssignedExtendVars) $extendVars = $this->_extend_variables; else $extendVars = array();
        if ($isAssignedExtendConst) $extendConsts = $this->extend_constants; else $extendConsts = array();

        $smartyVars = array_merge_recursive($this->base_constants, $extendConsts, $extendVars, (array)$smartyVars);
        foreach ($smartyVars as $key => $value) $this->_smarty->assign($key, $value);

        $this->_smarty->display($TPLfile);
    }
    /*-END READ-ONLY PROPERTIES------------------------------------------------------------------*/

    /** CONSTRUCTOR METHOD
     * @param string $path_to_smarty_templates: relative path the the templates directory of smarty (requried)
     * @param string $path_to_smarty_templates_c: relative path the the templates_c directory of smarty (required)
     * @param string $path_to_smarty_cache: relative path the the cache directory of smarty (null to exclude this directory)
     * @param string $path_to_smarty_configs: relative path the the configs directory of smarty (null to exclude this directory)
     * @param string $path_to_smarty_class: absolute path to the smarty.class.php file, or null to set to default path (assigned to $base_constants['path']['libs'] in configs.php file)
     * @param string|array $path_to_factory_configs: relative path to single or multiple configs.php file(s), or null to sest to default path
     */
    public function __construct(
            $path_to_smarty_templates   = "/smarty/templates",
            $path_to_smarty_templates_c = "/smarty/templates_c",
            $path_to_smarty_cache       = "/smarty/cache",
            $path_to_smarty_configs     = "/smarty/configs",
            $path_to_smarty_class       = null,
            $path_to_factory_configs    = null
        ) {

        if ($path_to_factory_configs == null) parent::__construct(); else parent::__construct($path_to_factory_configs);

        if ($path_to_smarty_class == null) require_once($this->base_constants["path"]["libs"] . "/smarty/smarty.class.php");
        else require_once($path_to_smarty_class);

        $this->_smarty = new Smarty();

        $this->_smarty->template_dir = $this->_root_path . strval($path_to_smarty_templates);
        $this->_smarty->compile_dir  = $this->_root_path . strval($path_to_smarty_templates_c);
        if (is_string($path_to_smarty_cache))   $this->_smarty->cache_dir  = $this->_root_path . $path_to_smarty_cache;
        if (is_string($path_to_smarty_configs)) $this->_smarty->config_dir = $this->_root_path . $path_to_smarty_configs;
    }
}


#######################################################################################################################
## DATABASE PART - EDITABLE ###########################################################################################
#######################################################################################################################


class base_model {
	protected $_server   = "";
	protected $_username = "";
	protected $_password = "";
	protected $_database = "";

	public function connect() {
		if (!$link = mysql_connect($this->_server, $this->_username, $this->_password)) die('Could not connect: ' . mysql_error());
		if (!$db_selected = mysql_select_db($this->_database, $link)) die ("Can't use database : " . mysql_error());
	}
}
?>